package com.weiyin.examination.application;

/**
 * @Title: CrashHandler.java
 * @Description: 程序crash
 * @author njf
 * @date 2017年9月21日
 * @version V1.0 Copyright (c) 2017 Company,Inc. All Rights Reserved.
 */

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Environment;

import com.weiyin.examination.activity.TopicActivity;
import com.weiyin.examination.util.LogUtil;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.Date;

public class CrashHandler implements UncaughtExceptionHandler {
    private static final String TAG = CrashHandler.class.getSimpleName();
    private static CrashHandler leapFrogCrashHandler;
    /**
     * 日志文件在sdcard中的路径
     */
    private static String CRASH_PATH_SDCARD_DIR = "/sdcard/examination/crash/";

    private CrashHandler() {

    }

    private Context context;

    public synchronized static CrashHandler getInstance() {
        if (leapFrogCrashHandler == null) {
            leapFrogCrashHandler = new CrashHandler();
            return leapFrogCrashHandler;
        } else {
            return leapFrogCrashHandler;
        }
    }

    public void init(Context context) {
        this.context = context;
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            // 在throwable的参数里面保存的有程序的异常信息
            StringBuilder sb = new StringBuilder();
            //1.得到手机的版本信息 硬件信息
            Field[] fields = Build.class.getDeclaredFields();
            for (Field filed : fields) {
                //暴力反射
                filed.setAccessible(true);
                String name = filed.getName();
                String value = filed.get(null).toString();
                sb.append(name);
                sb.append("=");
                sb.append(value);
                sb.append("\n");
            }


            //2.得到当前程序的版本号
            PackageInfo info =
                    context.getPackageManager().getPackageInfo(context.getPackageName(),
                            0);
            sb.append(info.versionName);
            sb.append("\n");
            //3.得到当前程序的异常信息
            Writer writer = new StringWriter();
            PrintWriter printwriter = new PrintWriter(writer);

            ex.printStackTrace(printwriter);
            printwriter.flush();
            printwriter.close();

            sb.append(writer.toString());

            // 4.提交异常信息到服务器
            LogUtil.e(TAG, sb.toString());
            // System.out.println(sb.toString());
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        writeToSDCard(ex);
        Constant.showTextToast(context, "程序跑去弹琴了，请重新打开下");
        App.getInstance().exit();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);

//		new Thread() {
//
//			@Override
//			public void run() {
//				//Looper.prepare();
//
//				//Looper.loop();
//
//			}
//
//		}.start();
//
//		new Thread() {
//
//			@Override
//			public void run() {
//				try {
//					Thread.sleep(2000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//
//			}
//
//		}.start();

//		restartApp();
    }

    public void restartApp() {
        Intent intent = new Intent(context, TopicActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
    }

    private boolean writeToSDCard(Throwable ex) {
        boolean isDealing = false;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            RandomAccessFile randomAccessFile = null;
            try {
                String fileName = CRASH_PATH_SDCARD_DIR;
                File file = new File(fileName);
                if (!file.exists())
                    file.mkdirs();
                randomAccessFile = new RandomAccessFile(fileName
                        + parseTime(System.currentTimeMillis()) + ".log", "rw");
                long fileLength = randomAccessFile.length();
                randomAccessFile.seek(fileLength);
                randomAccessFile.writeBytes(getThrowableInfo(ex));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                        isDealing = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return isDealing;
    }

    private String parseTime(long currentTimeMillis) {
        // TODO Auto-generated method stub
        Date date = new Date(currentTimeMillis);
        return date.toString();
    }

    private static String getThrowableInfo(Throwable ex) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        ex.printStackTrace(printWriter);
        return stringWriter.toString();
    }

}
